package com.vben.common.satoken.config;

import cn.dev33.satoken.stp.StpInterface;
import cn.hutool.core.collection.CollUtil;
import com.vben.common.core.constant.CacheKeyConstant;
import com.vben.common.redis.service.RedisService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

/**
 * 自定义权限验证接口扩展
 */
@Component
public class StpInterfaceConfig implements StpInterface {

    @Resource
    private RedisService redisService;

    /**
     * 获取账号对应的权限列表
     *
     * @param loginId   账号id
     * @param loginType 账号类型
     * @return 账号对应的权限列表
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        Optional<List<String>> permissionOptional = redisService.getList(CacheKeyConstant.USER_PERMISSION_LIST + loginId);
        return permissionOptional.orElse(CollUtil.newArrayList());
    }

    /**
     * 获取账号对应的角色列表
     *
     * @param loginId   账号id
     * @param loginType 账号类型
     * @return 账号对应的角色列表
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        Optional<List<String>> roleOptional = redisService.getList(CacheKeyConstant.USER_ROLE_LIST + loginId);
        return roleOptional.orElse(CollUtil.newArrayList());
    }

}
